package com.balloon.springboot.datasource.datasourceholder;

import lombok.Data;

import java.util.HashSet;

/**
 * 数据源ContextHolder
 *
 * @author liaofuxing
 * @data 2021/9/18
 */
@Data
public class DataSourceContextHolder {

    private static final HashSet<String> dataSourceSet = new HashSet<>();

    private static final ThreadLocal<String> dataSourceHolder = new ThreadLocal<>();

    public static void setDataSourceHolder(String supportDataSourceRouteKey) {
        dataSourceHolder.set(supportDataSourceRouteKey);
    }
    

    /**
     * 获取当前数据源名称
     *
     * @return 数据源key名称
     */
    public static String getDataSourceHolder() {
        return dataSourceHolder.get();
    }

    /**
     * 添加数据源路由
     *
     * @param supportDataSourceRouteKey 数据源key名称
     */
    public static void addDataSource(String supportDataSourceRouteKey) {
        dataSourceSet.add(supportDataSourceRouteKey);
    }

    /**
     * 获取当期应用所支持的所有数据源
     *
     * @return HashSet<String> 数据源集合
     */
    public static HashSet<String> getDataSourceSet() {
        return dataSourceSet;
    }

    /**
     * 清除上下文数据
     */
    public static void clear() {
        dataSourceHolder.remove();
    }

}